package Base;

/**
 * <p>Title SortUtil</p>
 *
 * @author YiMing Liu
 * @date 2018/7/11 20:18
 * <p>Description:</p>
 */
public class SortUtil {

    /**
     * 冒泡排序
     *
     * @param a
     * @param n
     */
    public static void bubbleSort(int[] a, int n) {
        for (int i = n - 1; i >= 1; i--) {
            bubble(a, n);
        }
    }

    public static void bubble(int[] a, int n) {
        for (int i = 0; i < n - 1; i++) {
            if (a[i] > a[i + 1]) {
                int temp = a[i];
                a[i] = a[i + 1];
                a[i + 1] = temp;
            }
        }

    }

    /**
     * 选择排序法
     *
     * @param a
     * @param n
     */
    public static void selectSort(int[] a, int n) {
        while (n > 1) {
            int maxIndex = findMaxIndex(a, n);
            int temp = a[maxIndex];
            a[maxIndex] = a[n - 1];
            a[n - 1] = temp;
            n--;
        }
    }

    public static int findMaxIndex(int[] a, int n) {
        int max = a[0];
        int maxIndex = 0;
        for (int i = 1; i < n; i++) {
            if (max < a[i]) {
                max = a[i];
                maxIndex++;
            }
        }
        return maxIndex;
    }

    /**
     * 插入排序
     * @param a
     */

    public static void insertSorr(int[] a) {
        for (int i = 1; i < a.length; i++) {
            int key = a[i];
            int j = i - 1;
            while ((j >= 0) && key < a[j]) {
                a[j + 1] = a[j];
                j--;
            }
            a[j + 1] = key;
        }
    }


    /**
     * 快速排序
     * @param a
     */
    public static void quicklySort(int [] a ){
        
    }
}
